package com.heima.leadnews.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.leadnews.admin.mapper.AdSensitiveMapper;
import com.heima.leadnews.admin.service.AdSensitiveService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.sensitive.dto.AdSensitiveDto;
import com.heima.model.sensitive.pojo.AdSensitive;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * 敏感词功能
 */
@Service
@Transactional
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper,AdSensitive> implements AdSensitiveService {

    /**
     * 敏感词列表查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listSensitive(AdSensitiveDto dto) {
        //1,判断查询数据是否为空
        if(dto==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //页数默认值
        dto.checkParam();

        //2,构建分页查询器
        IPage<AdSensitive> page=new Page<>(dto.getPage(),dto.getSize());

        //3,构建数据库查询器
        LambdaQueryWrapper<AdSensitive> adSensitiveQw=new LambdaQueryWrapper<>();

        //判断是否使用关键词查询
        if(StringUtils.isNotBlank(dto.getName())){
            adSensitiveQw.like(AdSensitive::getSensitives,dto.getName());
        }

        //4,分页查询
        page(page,adSensitiveQw);

        //5,封装基本数据
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(),(int) page.getTotal());
        result.setData(page.getRecords());
        //6,返回列表数据
        return result;
    }


    /**
     * 新增敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1，判断数据是否为空
        if(adSensitive==null || StringUtils.isBlank(adSensitive.getSensitives())){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2，封装基本数据
        adSensitive.setCreatedTime(new Date());

        //3，判断db是否存在
        LambdaQueryWrapper<AdSensitive> adSensitiveLambdaQw=new LambdaQueryWrapper<>();
        adSensitiveLambdaQw.eq(AdSensitive::getSensitives,adSensitive.getSensitives());
        AdSensitive one = getOne(adSensitiveLambdaQw);
        if(one!=null){
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }

//        LambdaQueryWrapper<AdSensitive> adSensitiveQw=new LambdaQueryWrapper<>();
//        List<AdSensitive> adSensitiveList = list(adSensitiveQw);
//        for (AdSensitive sensitive : adSensitiveList) {
//            if(sensitive.getSensitives().equals(adSensitive.getSensitives())){
//                throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
//            }
//        }
        //4,添加到db
        save(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delById(Integer id) {
        //1,判断id是否为空
        if(id==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2,查询数据库是否存在id
        LambdaQueryWrapper<AdSensitive> adSensitiveQw=new LambdaQueryWrapper<>();
        adSensitiveQw.eq(AdSensitive::getId,id);

        AdSensitive sensitive = getOne(adSensitiveQw);
        if(sensitive.getId()==null){
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3，删除id
        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        //1,判断数据是否为空,是否存在
        if(adSensitive==null || StringUtils.isBlank(adSensitive.getSensitives())){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2,查询敏感词是否存在，如果存在，抛异常
        LambdaQueryWrapper<AdSensitive> adSensitiveQw=new LambdaQueryWrapper<>();
        adSensitiveQw.eq(AdSensitive::getSensitives,adSensitive.getSensitives());
        AdSensitive one = getOne(adSensitiveQw);
        if(one!=null){
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
        //3，修改数据
        updateById(adSensitive);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
